import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import config from './config'
const baseURL = config.baseURL
axios.defaults.timeout = 60000
axios.defaults.baseURL = config.proxy ? '/api' : baseURL


function get (url, params) {
  return new Promise((resolve, reject) => {
    NProgress.start()
    axios.get(url, {params}).then(res => {
      NProgress.done()
      resolve(res)
    }).catch(error => {
      NProgress.done()
      reject(error)
    })
  })
}

function post(url, data) {
  return new Promise((resolve, reject) => {
    NProgress.start()
    axios.post(url, data).then(res => {
      NProgress.done()
      resolve(res)
    }).catch(error => {
      NProgress.done()
      reject(error)
    })
  })
}

export { get, post, baseURL }